import { defineConfig, loadEnv } from 'vite'
import react from '@vitejs/plugin-react'
import legacy from '@vitejs/plugin-legacy';
import tailwindcss from '@tailwindcss/vite'
import { resolve } from 'path';

// https://vite.dev/config/
export default defineConfig((mode) => {
  const env = loadEnv(mode.mode, process.cwd() + '/env', '');
  console.log("process.cwd(): ", env.ENV, env.API_PREFIX_JCI, process.cwd() + '/env')
  return {
    envDir: 'env', // 环境变量的目录
    server: { port: 9527 },
    envPrefix: ['VITE_', 'API_'],
    plugins: [
      react(),
      // https://github.com/vitejs/vite/tree/main/packages/plugin-legacy
      legacy({
        renderLegacyChunks: false,
        modernPolyfills: true,
      }),
      tailwindcss(),
    ],
    build: {
      // 默认就是 dist，也可以指定其他路径
      outDir: 'dist',
    },
    define: {
      __MODE__: mode,
      __CWD__: JSON.stringify(process.cwd()),
      __APP_VERSION__: { ...mode, },
    },
    resolve: {
      alias: {
        "@": resolve(__dirname, "./src"),
      },
      extensions: ['.mjs', '.js', '.mts', '.ts', '.jsx', '.tsx', '.json'],
    },
  }
})